package MovieFinderApp.MCMovieLister;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import MovieFinderApp.LCMovieInterfaces.IMovie;
import MovieFinderApp.MCMovieFinderInterfaces.IMovieFinder;
import MovieFinderApp.MCMovieListerInterfaces.*;


public class MovieLister implements IMovieLister, ICustomizeMovieLister{
	private IMovieFinder movieFinder;
	private Object[] movieArray;
	private PropertyChangeSupport changes = new PropertyChangeSupport( this );
	
	public MovieLister(){
		
	}
	
	public Object[] getMovieArray(){
		return this.movieArray;
	}
	
	public void setMovieFinder(IMovieFinder movieFinder){
		this.movieFinder = movieFinder;
	}

	
	/**Gibt Array mit Filmen von director zurueck
	 * 
	 * @param director
	 * @return - Movie[]
	 */
	public void moviesDirectedBy(String director){
		
		ArrayList<IMovie> movie_list = this.movieFinder.findAll();
		ArrayList<IMovie> new_movie_list = new ArrayList<IMovie>();
		for(IMovie movie: movie_list){
			if(movie.getDirector().equalsIgnoreCase(director) == true){
				new_movie_list.add(movie);
			}
		}
		
		IMovie[] movie_arr = new IMovie[new_movie_list.size()];
		new_movie_list.toArray(movie_arr);
		IMovie[] old = (IMovie[])this.movieArray;
		this.movieArray = movie_arr;
		changes.firePropertyChange("movieArray", old, this.movieArray);
	}
	 
	public void addPropertyChangeListener( PropertyChangeListener listener ){
		this.changes.addPropertyChangeListener(listener);
	}
	
	public void removePropertyChangeListener( PropertyChangeListener listener){
		this.changes.removePropertyChangeListener(listener);
	}
}
